package com.oa.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.oa.back.ResponseBack;
import com.oa.common.base.Page;
import com.oa.common.base.Pageable;
import com.oa.common.dto.AppsTypeDto;
import com.oa.common.enums.AppsFlowType;
import com.oa.common.enums.AppsStatus;
import com.oa.common.enums.AppsUserType;
import com.oa.common.enums.CodeStatus;
import com.oa.dao.AppsTypeDao;
import com.oa.dao.BaseDao;
import com.oa.entities.Department;
import com.oa.entities.Position;
import com.oa.entities.User;
import com.oa.entities.flows.AppType;
import com.oa.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.Set;

@Service
public class AppsTypeServiceImpl extends UndeleteServiceImpl<Long,AppType> implements AppsTypeService {

    @Autowired
    private AppsTypeDao dao;

    @Override
    protected BaseDao<Long, AppType> getSelfDao() {
        return dao;
    }

    @Autowired
    private UserService userService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private PositionService positionService;

    @Override
    public void setClass(Class<AppType> clazz) {

    }

    @Override
    public Page<AppType> findPage(Pageable pageable, String key,Integer type) {
        return dao.findPage(pageable,key,type);
    }

    @Override
    @Transactional
    public ResponseBack<String> submit(String params) {
        if (StringUtils.isEmpty(params)){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        AppsTypeDto dto= JSONObject.parseObject(params,AppsTypeDto.class);
        if (dto==null){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        if (StringUtils.isEmpty(dto.getTitle())){
            return ResponseBack.fial(CodeStatus.APPSTYPE_NAME_NOTNULL.getResponseStatus());
        }
        if (validate(dto.getTitle(),null)){
            return ResponseBack.fial(CodeStatus.APPSTYPE_EXIST.getResponseStatus());
        }
        AppType type= new AppType();
        type.setTitle(dto.getTitle());
        type.setAppStatus(dto.getAppStatus());
        type.setContext(dto.getContext());
        type.setFilePath(null);
        type.setNumber(System.currentTimeMillis());
        User isse= userService.getCurrent();
        type.setIssue(isse);
        type.setIssueId(isse.getId());
        Set<User> users= new HashSet<>();
        for (Long id: dto.getSupervisions()){
            users.add(userService.get(id));
        }
        type.setSupervisions(users);
        if(dto.getUseType()==AppsUserType.USER.getCode()){
            Set<User> userSet= new HashSet<>();
            for (Long id : dto.getUsers()){
                userSet.add(userService.get(id));
            }
            type.setUsers(userSet);
        }else if (dto.getUseType()==AppsUserType.DEPARTMENT.getCode()){
            Set<Department> departments=new HashSet<>();
            for (Long id: dto.getDepartments()){
                departments.add(departmentService.get(id));
            }
            type.setDepartments(departments);
        }else if (dto.getUseType()==AppsUserType.POSITION.getCode()){
            Set<Position> positions= new HashSet<>();
            for (Long id: dto.getPositions()){
                positions.add(positionService.get(id));
            }
            type.setPositions(positions);
        }
        type.setUseType(dto.getUseType());
        type.setAppStatus(AppsStatus.NORMAL.getCode());
        type.setFileType(dto.getFileType());
        type.setFlowType(AppsFlowType.FREEDOM.getCode());
        save(type);
        dao.createTable(type.getId());
        return ResponseBack.success(null);
    }

    @Override
    public Boolean validate(String title, Long id) {
        return dao.validate(title, id);
    }
}
